import grpc
import requests

from common.encry_decry import AesEncrypt
from common.file_load import load_yaml_file
from common.logger import GetLogger


class RequestsClient:
    # 使用类属性来定义一个session，他将作为所有接口发起的全局对象
    session = requests.session()

    def __init__(self):
        # 对于一个接口来说，他都有那些属性信息
        self.logger = GetLogger.get_logger()
        self.url = None
        self.method = None
        self.headers = None
        self.params = None
        self.data = None
        self.json = None
        self.files = None
        self.resp = None
        self.aes = AesEncrypt(key='0123456789012345')

    def send(self, **kwargs):
        # **kwargs 表示不定长的参数，意味着某个接口的发起调用可以不传任何参数
        # 那么不传任何参数我们认为要使用该接口的默认数据进行调用
        if 'url' not in kwargs:
            kwargs['url'] = self.url
        if 'method' not in kwargs:
            kwargs['method'] = self.method
        if 'headers' not in kwargs:
            kwargs['headers'] = self.headers
        if 'params' not in kwargs:
            kwargs['params'] = self.params
        if 'data' not in kwargs:
            kwargs['data'] = self.data
        if 'json' not in kwargs:
            kwargs['json'] = self.json
        if 'files' not in kwargs:
            kwargs['files'] = self.files
        # 接口请求数据日志收集
        for key, value in kwargs.items():
            self.logger.info(f'接口的{key}是:{value}')

        # 在这里按照你们的加密规则去处理加密数据
        # 比如你们针对data数据进行加密，json.dumps会将字典形式转成json格式字符串
        # kwargs['data'] = self.aes.encrypt(json.dumps(kwargs['data']))

        try:
            self.resp = RequestsClient.session.request(**kwargs)
            self.logger.info(f'接口请求头是:{self.resp.headers}')
            self.logger.info(f'kwargs是:{kwargs}')
            self.logger.info(f'接口响应状态码是:{self.resp.status_code}')
            self.logger.info(f'接口响应信息是:{self.resp.text}')
        except BaseException as e:
            self.logger.exception('接口发起报错')  # 这句会把异常记录在日志中
            raise BaseException(f'接口发起报错:{e}')  # 这句抛出异常，终止执行
        return self.resp

    # 单独提供一个解密响应数据的方法
    def get_resp_text(self):
        # return self.aes.decrypt(self.resp.text)
        return self.resp.text


class GrpcChannel:
    channel = None

    def __init__(self):
        self.host = load_yaml_file('/config/grpc.yml')['host']
        GrpcChannel.channel = grpc.insecure_channel(self.host)


class BaseDubbo:

    def __init__(self):
        self.host = load_yaml_file('/config/dubbo.yml')['host']
        self.dubbo_version = load_yaml_file('/config/dubbo.yml')['dubbo_version']
        self.version = load_yaml_file('/config/dubbo.yml')['version']

    def send(self, method, *args):
        res = self.service.call(method, args=args)
        return res


class WebsocketClient:

    def __init__(self):
        self.host = load_yaml_file('/config/websocket.yml')['host']
        self.logger = GetLogger.logger

    def send(self, params):
        try:
            self.ws.send(params)
            self.logger.info(f'发送数据成功:{params}')
        except:
            self.logger.exception('发送数据失败')

    def recv(self):
        try:
            res = self.ws.recv()
            self.logger.info(f'数据接收成功:{res}')
            return res
        except:
            self.logger.exception('接收数据失败')
            return ''
